/*
 * Copyright (c) 2016-2021, Hangzhou Qulian Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef _LARGEFILE_H_
#define _LARGEFILE_H_

#include "ql_type.h"
#include "log.h"
#include "ql_error_type.h"
#include "md5.h"

// ----------------------------------------------
// Error type definition
// ----------------------------------------------
#define FILE_SUCCESS               (ERR_LARGEFILE_BASE + 0)  // Success
#define FILE_ERR_CALLOC_MEMORY    -(ERR_LARGEFILE_BASE + 1)  // Failed to apply for memory
#define FILE_ERR_NOT_EXIST        -(ERR_LARGEFILE_BASE + 2)  // Failed to open file
#define FILE_ERR_PARAM_INVAILD    -(ERR_LARGEFILE_BASE + 3)  // Invalid argument
#define FILE_ERR_BUF_TOO_SMALL    -(ERR_LARGEFILE_BASE + 4)  // Too little space

// ----------------------------------------------
// type definition
// ----------------------------------------------
// -- large file structure --
typedef struct {
    OhByteType   *path;
    OhByteType   hash[33];       // file hash, string format
    OhByteType   *name;          // file name, string format
    OhSizeType   size;           // file size
    OhByteType   *node;          // node list
    OhByteType   *user;          // user list
    OhByteType   *des;           // file description, string format
} LargefileType;

/*
 Create file object.
 Inputs:
    path - file path. Need to be accurate to the file name.
    name - The name used when uploading the file.
    des  - The description used when uploading the file.
    node - Node whitelist, if it is empty, all nodes are visible
    user - User whitelist, if it is empty, it will be visible to all users
 Outputs:
    f    - file object.
 */
OhErrType  FileInfoNew(LargefileType* f, OhByteType *path, OhByteType *name, OhByteType *des, OhByteType *node, OhByteType *user);

/*
 Calculate file MD5 result. This method will open the file under <path>
 Inputs:
    f    - file object.
 */
OhErrType  FileCalMd5(LargefileType* f);

/*
 Save the file hash result to <buf>.
 If <bufLen> is less than the required length, it cannot be saved.
 Inputs:
    f       - file object.
    bufLen - The length of buf.
 Outputs:
    buf     - Storage buffer.
 */
OhErrType  FileGetHash(LargefileType* f, OhByteType* buf, OhSizeType bufLen);

/*
 Format file information into extra string format.
 Inputs:
    f       - file object.
    bufLen - The length of buf.
 Outputs:
    buf     - Storage buffer.
 */
OhErrType  FileInfoFormat(LargefileType* f, OhByteType* buf, OhSizeType bufLen);

/*
 Free file object.
 Inputs:
    f    - file object.
 */
OhVoidType FileInfoFree(LargefileType* f);

#endif
